package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;

import cn.hutool.core.lang.UUID;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.dto.CoachResumeDto;
import com.woniu.model.entity.YCoachResume;
import com.woniu.model.form.AddCoachResumeForm;
import com.woniu.model.form.UpdateCoachImageForm;
import com.woniu.model.form.UpdateCoachMessageForm;
import com.woniu.model.param.*;
import com.woniu.service.YCoachResumeService;
import com.woniu.util.FileUtil;
import com.woniu.util.OSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLOutput;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 胡歌
 * @since 2024年05月22日
 */
@RestController
@RequestMapping("/yCoachResume")
@Api(tags = "教练操作简历的一些接口")
public class YCoachResumeController {
    @Resource
    private YCoachResumeService coachResumeService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private OSSUtil ossUtil;














    /**
     *
     * @param form  新增教练简历 传入教练简历需要的参数
     * @return
     * @throws Exception
     */
    @ApiOperation("新增教练简历")
    @PostMapping("/addCoachResume")
    @Transactional
    public Result addCoachResume( AddCoachResumeForm form, @RequestHeader String token)throws Exception{
        if (!FileUtil.isImageFile(form.getImage().getInputStream())){
            throw  new YogaException(9999,"照片文件类型错误");
        }
        if (!FileUtil.isImageFile(form.getCardImage().getInputStream())){
            throw  new YogaException(9998,"执业证书文件类型错误");
        }
        if (!FileUtil.isImageFile(form.getHonor().getInputStream())){
            throw  new YogaException(9997,"获奖证书文件类型错误");
        }
        //照片路径
        String oldName = form.getImage().getOriginalFilename();
        String suffix = oldName.substring(oldName.lastIndexOf("."));
        String name = UUID.randomUUID().toString();
        String filePath = ossUtil.uploadFile(form.getImage().getInputStream(), name + suffix, "woniupengyuyan");
        //执业证书路径
        String oldName1 = form.getCardImage().getOriginalFilename();
        String suffix1 = oldName1.substring(oldName1.lastIndexOf("."));
        String name1 = UUID.randomUUID().toString();
        String filePath1 = ossUtil.uploadFile(form.getCardImage().getInputStream(), name1 + suffix1, "woniupengyuyan");
        //获奖证书路径
        String oldName2 = form.getHonor().getOriginalFilename();
        String suffix2 = oldName2.substring(oldName2.lastIndexOf("."));
        String name2 = UUID.randomUUID().toString();
        String filePath2 = ossUtil.uploadFile(form.getHonor().getInputStream(), name2 + suffix2, "woniupengyuyan");

        AddCoachResumeParam param = BeanUtil.toBean(form, AddCoachResumeParam.class);
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());

        param.setCoachId(coachId);
        param.setImage(filePath);
        param.setCardImage(filePath1);
        param.setHonor(filePath2);

        coachResumeService.addCoachResume(param);
        return Result.getResult().setMsg("新增教练简历成功");
    }










    /**
     *教练修改个人简历照片  传入图片地址转换为字符串
     * @param
     * @return
     */
    @PostMapping("/updateImage")
    @ApiOperation("教练修改个人简历照片")
    public Result updateImage(UpdateCoachImageForm form, @RequestHeader String token)throws Exception{
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());
        if (!FileUtil.isImageFile(form.getImage().getInputStream())){
            throw  new YogaException(9999,"文件类型错误");
        }
        String oldName = form.getImage().getOriginalFilename();
        String suffix = oldName.substring(oldName.lastIndexOf("."));
        String name = UUID.randomUUID().toString();
        String filePath = ossUtil.uploadFile(form.getImage().getInputStream(), name + suffix, "woniupengyuyan");
        UpdateCoachImageParam param = BeanUtil.toBean(form, UpdateCoachImageParam.class);
        param.setCoachId(coachId);
        param.setImage(filePath);
        coachResumeService.updateImage(param);
        return Result.getResult().setMsg("修改个人头像成功");
    }

    /**
     *教练修改个人简历职业证书照片  传入图片地址转换为字符串
     * @param
     * @return
     */
    @PostMapping("/updateCardImage")
    @ApiOperation("教练修改个人简历职业证书照片")
    public Result updateCardImage(UpdateCoachImageForm form, @RequestHeader String token)throws Exception{
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());
        if (!FileUtil.isImageFile(form.getImage().getInputStream())){
            throw  new YogaException(9999,"文件类型错误");
        }
        String oldName = form.getImage().getOriginalFilename();
        String suffix = oldName.substring(oldName.lastIndexOf("."));
        String name = UUID.randomUUID().toString();
        String filePath = ossUtil.uploadFile(form.getImage().getInputStream(), name + suffix, "woniupengyuyan");
        updateCardImageParam param = BeanUtil.toBean(form, updateCardImageParam.class);
        param.setCoachId(coachId);
        param.setCardImage(filePath);
        coachResumeService.updateCardImage(param);
        return Result.getResult().setMsg("修改个人头像成功");
    }

    /**
     *教练修改个人简历获奖证书照片  传入图片地址转换为字符串
     * @param
     * @return
     */
    @PostMapping("/updateHonor")
    @ApiOperation("教练修改个人简历获奖证书照片")
    public Result updateHonor(UpdateCoachImageForm form, @RequestHeader String token)throws Exception{
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());
        if (!FileUtil.isImageFile(form.getImage().getInputStream())){
            throw  new YogaException(9999,"文件类型错误");
        }
        String oldName = form.getImage().getOriginalFilename();
        String suffix = oldName.substring(oldName.lastIndexOf("."));
        String name = UUID.randomUUID().toString();
        String filePath = ossUtil.uploadFile(form.getImage().getInputStream(), name + suffix, "woniupengyuyan");
        updateHonorParam param = BeanUtil.toBean(form, updateHonorParam.class);
        param.setCoachId(coachId);
        param.setHonor(filePath);
        coachResumeService.updateHonor(param);
        return Result.getResult().setMsg("修改个人头像成功");
    }

    /**
     *教练修改个人简历信息 可以修改简历信息中的jobResume（工作履历） age（年龄） phone（联系电话） address（地址）
     * @param
     * @return
     */
    @Transactional
    @PostMapping("/updateCoachMessage")
    @ApiOperation("修改个人简历信息")
    public  Result updateMessage(@RequestBody UpdateCoachMessageForm form, @RequestHeader String token)throws Exception{
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());
        UpdateCoachMessageParam param = BeanUtil.toBean(form, UpdateCoachMessageParam.class);
        param.setCoachId(userId);
        YCoachResume coachResume = coachResumeService.updateMessage(param);
        return Result.newInstance(YCoachResume.class).setData(coachResume).setMsg("修改成功");
    }

    /**
     * 展示简历详情
     * @return
     */
    @GetMapping("/coachResumeList")
    @ApiOperation("展示简历详情")
    public Result<List<CoachResumeDto>> coachResumeList(){
        List<CoachResumeDto> list=coachResumeService.coachResumeList();
        return Result.newListInstance(CoachResumeDto.class).setData(list);
    }


    /**
     * 根据token里面传的教练id查询自己的简历回去
     * @return
     */
    @GetMapping("/loadCoachResume")
    @ApiOperation("根据token里面传的教练id查询自己的简历回去")
    public Result<YCoachResume> loadCoachResume( @RequestHeader String token ){
        String refreshToken = redisUtil.get(token);
        Integer CoachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());

        YCoachResume yCoachResume = coachResumeService.loadCoachResume(CoachId);

        return Result.newInstance(YCoachResume.class).setData(yCoachResume).setCode(200).setMsg("查询成功");
    }






    /**
     * 根据id查询教练简历
     * @param
     * @return
     */
    @GetMapping("/yCoachResumes/{coachId}")
    @ApiOperation("根据id查询教练简历")
    public Result<CoachResumeDto> yCoachResume(@PathVariable("coachId") Integer coachId){
        YCoachResume coachResume = coachResumeService.yCoachResume(coachId);
        return Result.newInstance(CoachResumeDto.class).setData(BeanUtil.toBean(coachResume,CoachResumeDto.class));
    }



    /**
     * 根据id查询教练简历
     * @param
     * @return
     */
    @GetMapping("/VenueLookCoachResume")
    @ApiOperation("根据id查询教练简历")
    public Result<CoachResumeDto> VenueLookCoachResume( Integer coachId){
        YCoachResume coachResume = coachResumeService.yCoachResume(coachId);
        return Result.newInstance(CoachResumeDto.class).setData(BeanUtil.toBean(coachResume,CoachResumeDto.class));
    }






}

